#!/bin/bash -x

export NCARG_ROOT="/public/software/NCL_6.1.0"
export PATH="$PATH:$NCARG_ROOT/bin"

cd $HOME/Codes/WRF_scripts/WRF_Post
echo $#
if [ $# -lt 1 ]; then
    start_date=`date -d "-1 day" "+%Y%m%d"`20
    echo "Processing Date: $start_date"
else
    start_date=$1
fi

wrfout_dir="/public/users/wrf/wrfout/$start_date/"

wrfpost_dir="$HOME/wrfpost"
wrfpost_pdf_dir="$wrfpost_dir/pdf"
wrfpost_pdf_surf_dir="$wrfpost_pdf_dir/surface"
wrfpost_pdf_prcp_dir="$wrfpost_pdf_dir/precipitation"
wrfpost_png_dir="$wrfpost_dir/png/${start_date}"

mkdir -p $wrfpost_pdf_surf_dir
mkdir -p $wrfpost_pdf_prcp_dir

# grid precipitation
# domain_list="d01 d02"
# for domain in $domain_list
# do
#     wrfout_file=`ls ${wrfout_dir}/wrfout_${domain}*`
#     path_txt_precip="$wrfpost_dir/txt/grid_precip/$start_date/$domain"
#     mkdir -p $path_txt_precip
#     ncl input_file='"'$wrfout_file'"' output_prefix='"'$path_txt_precip/precip_${domain}_'"' wrfout_to_grid_precip.ncl
# done

# station precip
domain_list="d02"
ylj_configFile="/public/users/wrf/Codes/WRF_scripts/WRF_Post/StationLists/YLJ_precip_stnlist.csv"
wj_configFile="/public/users/wrf/Codes/WRF_scripts/WRF_Post/StationLists/WJ_precip_stnlist.csv"
for domain in $domain_list
do
    wrfout_file=`ls ${wrfout_dir}/wrfout_${domain}*`
    path_station_precip_ylj="$wrfpost_dir/txt/station_precip/ylj/$start_date/"
    path_station_precip_wj="$wrfpost_dir/txt/station_precip/wj/$start_date/"
    mkdir -p $path_station_precip_ylj
    mkdir -p $path_station_precip_wj
    ncl input_file='"'$wrfout_file'"' output_prefix='"'$path_station_precip_ylj'"' configFile='"'$ylj_configFile'"' wrfout_to_station_precip.ncl
    ncl input_file='"'$wrfout_file'"' output_prefix='"'$path_station_precip_wj'"' configFile='"'$wj_configFile'"' wrfout_to_station_precip.ncl
done

# grid radiation
# domain_list="d01 d02"
# for domain in $domain_list
# do
    # wrfout_file=`ls ${wrfout_dir}/wrfout_${domain}*`
    # path_txt_sswdn="$wrfpost_dir/txt/grid_sswdn/$start_date/$domain"
    # mkdir -p $path_txt_sswdn
    # ncl input_file='"'$wrfout_file'"' output_prefix='"'$path_txt_sswdn/sswdn_${domain}_'"' wrfout_to_grid_sswdn.ncl
# done

# plot contours for 300/500hPa, surface wind and precipitation

domain_list="d01 d02"

for domain in $domain_list
do
    wrfout_file=`ls ${wrfout_dir}/wrfout_${domain}*`

    # get station radiation from wrfout
    sswdn_configFile="/public/users/wrf/Codes/WRF_scripts/WRF_Post/StationLists/sswdn_stnlist.csv"
    station_sswdn_dir="$wrfpost_dir/txt/station_sswdn/$start_date/$domain/"
    mkdir -p $station_sswdn_dir
    ncl configFile='"'$sswdn_configFile'"' input_file='"'$wrfout_file'"' output_prefix='"'$station_sswdn_dir'"' wrfout_to_station_sswdn.ncl

    # get station wind from wrfout
    wind_configFile="/public/users/wrf/Codes/WRF_scripts/WRF_Post/StationLists/wind_stnlist.csv"
    station_wind_dir="$wrfpost_dir/txt/station_wind/$start_date/$domain/"
    mkdir -p $station_wind_dir
    ncl configFile='"'$wind_configFile'"' input_file='"'$wrfout_file'"' output_prefix='"'$station_wind_dir'"' wrfout_to_station_wind.ncl

    # get station all values(including wind, radiation, precip, temp, etc)  from wrfout
    allvalue_configFile="/public/users/wrf/Codes/WRF_scripts/WRF_Post/StationLists/allvalue_stnlist.csv"
    station_allvalue_dir="$wrfpost_dir/txt/station_allvalue/$domain/"
    mkdir -p $station_allvalue_dir
    ncl configFile='"'$allvalue_configFile'"' input_file='"'$wrfout_file'"' output_prefix='"'$station_allvalue_dir'"' wrfout_to_station_allvalue.ncl


    # plot surface wind, pressure and temperature
    path_surface="$wrfpost_dir/png/$start_date/surface/$domain"
    mkdir -p $path_surface
    pdf_surface_file="$path_surface/surface_${domain}_${start_date}_"
    ncl input_file='"'$wrfout_file'"' output_prefix='"'$pdf_surface_file'"' wrf_surface.ncl

    # 500hPa plot 
    path_500hpa="$wrfpost_dir/png/$start_date/500hpa/$domain"
    mkdir -p $path_500hpa
    pdf_500hpa_file="$path_500hpa/500hpa_${domain}_${start_date}_"
    ncl input_file='"'$wrfout_file'"' output_prefix='"'$pdf_500hpa_file'"' wrf_500hpa.ncl

    # 300hPa plot 
    path_300hpa="$wrfpost_dir/png/$start_date/300hpa/$domain"
    mkdir -p $path_300hpa
    pdf_300hpa_file="$path_300hpa/300hpa_${domain}_${start_date}_"
    ncl input_file='"'$wrfout_file'"' output_prefix='"'$pdf_300hpa_file'"' wrf_300hpa.ncl

    # 24h precipitation
    path_precip24="$wrfpost_dir/png/$start_date/precip24/$domain"
    mkdir -p $path_precip24
    pdf_precip24_file="$path_precip24/precip24_${domain}_${start_date}_"
    ncl input_file='"'$wrfout_file'"' output_prefix='"'$pdf_precip24_file'"' wrf_precip24.ncl

    # 1h radiation
    path_sswdn="$wrfpost_dir/png/$start_date/sswdn/$domain"
    mkdir -p $path_sswdn
    output_prefix_sswdn="$path_sswdn/sswdn_${domain}_${start_date}_"
    ncl input_file='"'$wrfout_file'"' output_prefix='"'$output_prefix_sswdn'"' wrf_sswdn.ncl

    # surface wind
    path_sfcwind="$wrfpost_dir/png/$start_date/sfcwind/$domain"
    mkdir -p $path_sfcwind
    output_prefix_sfcwind="$path_sfcwind/sfcwind_${domain}_${start_date}_"
    ncl input_file='"'$wrfout_file'"' output_prefix='"'$output_prefix_sfcwind'"' wrf_sfcwind.ncl
done
